//
//  ChangePasswordViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/6.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "PublicChangePwdApi.h"

@interface ChangePasswordViewController ()

@property (strong, nonatomic)  UITextField *oldPasswordTF;
@property (strong, nonatomic)  UITextField *theNewPasswordTF;
@property (strong, nonatomic)  UITextField *confirmPasswordTF;
@property (strong, nonatomic) UIButton *sureButton;

@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)refreshBaseControlValue {
    
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    [self setNavigationTitle:@"修改密码" titleColor:[UIColor blackColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
    
    [self masLayoutSubviews];
}



#pragma mark -- 点击事件
- (void) didClickSureButton{
    if (self.oldPasswordTF.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"原密码不能为空"];
        return;
    }
    if (self.theNewPasswordTF.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"新密码不能为空"];
        return;
    }
    if (self.confirmPasswordTF.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"重复密码不能为空"];
        return;
    }
    
    if (![self.theNewPasswordTF.text isEqualToString:self.confirmPasswordTF.text]) {
        [SVProgressHUD showInfoWithStatus:@"新密码和重复密码要一样"];
        return;
    }
    if (![self.theNewPasswordTF.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-18位字母或数字密码\n不包含除_@.之外的特殊符号"];
        return;
    }
    PublicChangePwdApi *api = [[PublicChangePwdApi alloc]initWithDic:@{@"old_pwd":self.oldPasswordTF.text,@"new_pwd":self.theNewPasswordTF.text}];
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showSuccessWithStatus:@"密码修改成功，请重新登陆"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            DTCSAVE_OBJECT(@"NO", LOGIN);
            [[AppDelegate defaultService]setRootViewController];
        });

    } failure:nil];
    
}


#pragma mark -- 懒加载
- (UITextField *)oldPasswordTF
{
    if(!_oldPasswordTF)
    {
        UIView *superView = self.view;
        [superView addSubview:_oldPasswordTF];
        _oldPasswordTF = [[UITextField alloc]init];
        _oldPasswordTF.placeholder = @"请输入原密码";
        _oldPasswordTF.font = [UIFont systemFontOfSize:px1080Width(42)];
    }
    return _oldPasswordTF;
}
- (UITextField *)theNewPasswordTF
{
    if(!_theNewPasswordTF)
    {
        UIView *superView = self.view;
        [superView addSubview:_theNewPasswordTF];
        _theNewPasswordTF = [[UITextField alloc]init];
        _theNewPasswordTF.placeholder = @"请输入新密码";
        _theNewPasswordTF.font = [UIFont systemFontOfSize:px1080Width(42)];
    }
    return _theNewPasswordTF;
}
- (UITextField *)confirmPasswordTF
{
    if(!_confirmPasswordTF)
    {
        UIView *superView = self.view;
        [superView addSubview:_confirmPasswordTF];
        _confirmPasswordTF = [[UITextField alloc]init];
        _confirmPasswordTF.placeholder = @"请再输入新密码";
        _confirmPasswordTF.font = [UIFont systemFontOfSize:px1080Width(42)];
    }
    return _confirmPasswordTF;
}

- (UIButton *)sureButton
{
    if(!_sureButton)
    {
        _sureButton = [[UIButton alloc]init];
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_sureButton addTarget:self action:@selector(didClickSureButton) forControlEvents:UIControlEventTouchUpInside];
        _sureButton.layer.cornerRadius = 20;
        _sureButton.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
        [self.view addSubview:_sureButton];
    }
    return _sureButton;
}


#pragma mark -- 初始化
- (void) masLayoutSubviews{
    int count = 3;
    UIView *lastView = nil;
    NSArray *array = @[self.oldPasswordTF,self.theNewPasswordTF,self.confirmPasswordTF];
    for(int i = 0;i < count;++i ){
        //创建一个View
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:view];
        
        //向subView添加约束
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self.view);
            make.height.mas_equalTo(px1920Height(200));
            
            //判断是否有前一个子View
            if ( lastView ) {
                //如果有前一个View，上边界和前一个View的下边界紧贴
                make.top.mas_equalTo(lastView.mas_bottom).with.offset(2);
            } else {
                //如果没有前一个View，上边界和container的下边界紧贴
                make.top.mas_equalTo(self.view.mas_top).with.offset(64);
            }
        }];
        
        [view addSubview:array[i]];
        [array[i] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(view);
            make.left.equalTo(self.view).with.offset(px1080Width(52));
            make.right.equalTo(self.view).with.offset(-px1080Width(52));
            make.height.mas_equalTo(px1920Height(200));
        }];
        
        lastView = view;
    }
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastView.mas_bottom).with.offset(px1920Height(224));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(px1080Width(500));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
}


@end
